const shop = require('../../../mixins/shop')
const mixins = require('../../../mixins/common')
const util = require('../../../utils/util');
const app = getApp()

const options = {

  /**
   * 页面的初始数据
   */
  data: {
    screenLiHeight: app.globalData.screenLiHeight,
    navbarTrans: 0,
    color: "0,0,0",
    iconTheme: "white",
    iconImg: "/image/back.png",
    iconImg2: "/image/shopping.png",
    id: '',
    goodsinfo: {},
    swiperCurrent: 1,
    serviceTxt: '',
    param: [],
    product: [],
    paraminfo: {
      id: 0,
      vip_price: '',
      price: '',
      pic: '',
      value: '',
      stock: 1,
    },
    num: 1,
    securityShow: false,
    detailsShow: false,
    showShare: false,
    goodsComment: [],
    cartCount: 0,
  },

  onPageScroll: function onPageScroll(e) {
    let that = this;
    var trans = (e.scrollTop > 55 ? 55 : e.scrollTop) / 55;
    var frontColor = "#ffffff";
    var iconTheme = "white";
    var iconImg = "/image/back.png";
    var iconImg2 = "/image/shopping.png";
    if (trans == 0) {
      frontColor = "#ffffff";
      iconTheme = "white";
      iconImg = "/image/back.png";
      iconImg2 = "/image/shopping.png";
    } else if (trans >= 0.4) {
      frontColor = "#000000";
      iconTheme = "black";
      iconImg = "/image/back-line.png";
      iconImg2 = "/image/shopping-line.png";
    }
    that.setData({
      navbarTrans: trans,
      iconTheme: iconTheme,
      iconImg: iconImg,
      iconImg2: iconImg2
    });
    wx.setNavigationBarColor({
      frontColor: frontColor,
      backgroundColor: frontColor,
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
  },

  toBack() {
    wx.navigateBack();
  },


  onLoad: function (options) {
    let that = this;
    that.setData({
      id: options.id
    })
    that.getShopGoodsDetails();
    that.getShopGoodsPosts();
  },
  onShow: function () {
    this.userCartCount();
  },

  swiperChange: function (e) {
    this.setData({
      swiperCurrent: e.detail.current + 1
    })
  },

  toDetails(e) {
    this.setData({
      detailsShow: true
    })
  },

  toDetailsClose(e) {
    this.setData({
      detailsShow: false
    })
  },

  toSecurity() {
    this.setData({
      securityShow: true
    })
  },

  toSecurityClose() {
    this.setData({
      securityShow: false
    })
  },

  numberTap(e) {
    this.setData({
      num: e.detail.count
    })
  },

  addCart(e) {
    let that = this;
    let paraminfo = that.data.paraminfo;
    let type = e.currentTarget.dataset.type;
    if (paraminfo.stock == 0) {
      wx.showToast({
        title: '该规格暂时没有库存啦！',
        icon: 'none',
        duration: 1500
      })
    } else {
      if (type == 0) {
        that.setData({
          detailsShow: true
        })
      } else {
        if (paraminfo.id) {
          that.getShopAddCart();
        } else {
          wx.showToast({
            title: '请选择规格！',
            icon: 'none',
            duration: 1500
          })
        }
      }
    }
  },

  toGoodsPosts() {
    wx.navigateTo({
      url: '/pages/shop/goods-details/posts?id=' + this.data.goodsinfo.id,
    })
  },

  paramTap(e) {
    let that = this;
    let idx = e.target.dataset.idx;
    let idxs = e.target.dataset.idxs;
    let value = e.target.dataset.value;
    let param = that.data.param;
    let product = that.data.product;
    let have = true;
    for (let i in product) {
      if (product[i].indx == idx) {
        product[i].value = value;
        have = false;
      }
    }
    if (have) {
      let args = {};
      args.indx = idx;
      args.value = value;
      product.push(args);
    }
    for (let v in param[idx].value) {
      if (v == idxs) {
        param[idx].value[v].state = true;
      } else {
        param[idx].value[v].state = false;
      }
    }
    that.setData({
      param: param,
      product: product
    })
    let arr = [];
    for (let i in product) {
      arr.push(product[i].value)
    }
    let result = util.getNumbers(arr, arr.length, false);
    let gproduct = that.data.goodsinfo.product;
    for (let i in gproduct) {
      for (let j in result) {
        if (gproduct[i].param_value == result[j]) {
          let args = {}
          let paraminfo = {}
          if (gproduct[i].pic != '' && gproduct[i].pic != null) {
            paraminfo.pic = gproduct[i].pic;
          }
          paraminfo.id = gproduct[i].id;
          paraminfo.price = gproduct[i].price;
          paraminfo.vip_price = gproduct[i].vip_price;
          paraminfo.value = gproduct[i].param_value;
          paraminfo.stock = gproduct[i].stock;
          args.paraminfo = paraminfo;
          that.setData(args)
        }
      }
    }
  },

  toShop() {
    wx.switchTab({
      url: '/pages/tabbar/shop/shop',
    })
  },

  toCart() {
    wx.navigateTo({
      url: '/pages/shop/cart/cart',
    })
  },

  shareGoodsClick() {
    let that = this;
    let postsGoods = [];
    let goods = {};
    goods.id = that.data.goodsinfo.id;
    goods.name = that.data.goodsinfo.name;
    goods.pic = that.data.goodsinfo.pic[0];
    postsGoods.push(goods);
    wx.setStorageSync('postsGoods', postsGoods);
    wx.navigateTo({
      url: '/pages/creat/index/creat',
    })
  },

  onShareAppMessage: function () {
    return {
      title: this.data.goodsinfo.name,
      path: '/pages/shop/goods-details/goods-details?id=' + this.data.goodsinfo.id,
      imageUrl: this.data.goodsinfo.pic[0],
    }
  }
}

shop(options)
mixins(options)
Page(options)